#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main()
{
    try
    {
        boost::asio::io_context io;

        // 监听端口 12345
        tcp::acceptor acceptor(io, tcp::endpoint(tcp::v4(), 12345));
        std::cout << "Server is listening on port 12345...\n";

        tcp::socket socket(io);
        acceptor.accept(socket);

        std::cout << "Client connected!\n";

        // 接收数据
        char data[1024];
        size_t len = socket.read_some(boost::asio::buffer(data));
        std::cout << "Received: " << std::string(data, len) << "\n";

        // 回复数据
        std::string reply = "Hello from server!";
        boost::asio::write(socket, boost::asio::buffer(reply));
    }
    catch (std::exception &e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }
}
